using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using mesoBoard.Data.Repositories;
using mesoBoard.Data;
using System.Web.Routing;
using mesoBoard.Web.Models;
using mesoBoard.Web.Core;
using mesoBoard.Web;
using System.IO;

namespace mesoBoard.Web.Controllers
{
    public partial class MembersController : mesoBoardController
    {
        public MembersController()
        {

        }

        public ActionResult ViewGroup(int GroupID, int PageSize = 10, int Page = 1)
        {
            Role group = Repositories.Roles.GetRole(GroupID);

            if (!group.IsGroup)
            {
                SetError("Invalid group ID");
                return RedirectToAction("Groups");
            }

            IEnumerable<User> usersInRole = group.InRoles.Select(x => x.User);

            int MemberCount = usersInRole.Count();
            int PageCount = (int)Math.Ceiling(((decimal)MemberCount / PageSize));

            usersInRole = usersInRole.Skip((Page - 1) * PageSize).Take(PageSize);

            RouteValueDictionary rvd = new RouteValueDictionary();
            rvd.Add("GroupID", GroupID);

            ViewData["Paging"] = new Paging(Page, PageCount, "ViewGroup", "Members", rvd); 
            
            ViewData["Users"] = usersInRole;

            return View(group);
        }

        public ActionResult Groups(int Page=1, int PageSize=10)
        {
            IEnumerable<Role> AllGroups = Repositories.Roles.GetAllRoles().Where(x => x.IsGroup == true);

            int MemberCount = AllGroups.Count();
            int PageCount = (int)Math.Ceiling(((decimal)MemberCount / PageSize));

            AllGroups = AllGroups.Skip((Page - 1) * PageSize).Take(PageSize);

            ViewData["Paging"] = new Paging(Page, PageCount, "Groups", "Members", null);

            return View(AllGroups);
        }

        public ActionResult MembersList(string Letter="(All)", int Page=1, int PageSize=10)
        {
            IEnumerable<User> Members = Repositories.Users.GetAllUsers();
            ViewData["letter"] = Letter;
            if (Letter != "(All)")
                Members = Members.Where(y => y.UsernameLower[0].ToString() == Letter.ToLower());

            int MemberCount = Members.Count();
            int PageCount = (int)Math.Ceiling(((decimal)MemberCount / PageSize));

            Members = Members.Skip((Page - 1) * PageSize).Take(PageSize).OrderBy(x => x.UsernameLower);
            RouteValueDictionary BaseValues = new RouteValueDictionary();
            BaseValues.Add("Letter", Letter);

            ViewData["Paging"] = new Paging(Page, PageCount, "MembersList", "Members", BaseValues);

            return View(Members);
        }

        public ActionResult UserProfile(string UserNameOrID)
        {
            int tryInt;
            User UProfile;

            if (int.TryParse(UserNameOrID, out tryInt))
                UProfile = mbServices.Users.GetUser(tryInt);
            else
                UProfile = mbServices.Users.GetUser(UserNameOrID);

            if (UProfile != null)
            {
                ViewData["ThreadCount"] = Repositories.Threads.GetAllThreads().Where(x => Repositories.Posts.GetPost(x.FirstPostID).UserID == UProfile.UserID).Count();// Repositories.Threads.GetAllThreads().Where(x => x.FirstPost.UserID == UProfile.UserID).Count();
                ViewData["LastPost"] = Repositories.Posts.GetUserLastPost(UProfile.UserID);
                ViewData["BreadCrumb"] = UProfile.Username + "'s Profile";
                return View(UProfile);
            }
            else
            {
                SetError("No such user found");
                return RedirectToAction("MembersList");
            }
        }

        public ActionResult Terms()
        {
            return View();
        }

        public ActionResult UserBirthdays()
        {
            return View(Repositories.Users.BirthdayUsers());
        }

        public ActionResult ChangeTheme(int PreviewTheme, string Action)
        {
            if (Action == "Preview")
            {
                Session.Add("ptheme", PreviewTheme);
                SetSuccess("Theme changed");
            }
            else if (Action == "Reset")
            {
                if (Session["ptheme"] != null)
                {
                    Session.Remove("ptheme");
                }
            }

            return Redirect(Request.UrlReferrer.AbsolutePath);
        }
    }
}
